//
//  ClassifyRightTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/31.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class ClassifyRightTableViewCell: UITableViewCell {
    
    @IBOutlet weak var xiajiaView: UIView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var price: UILabel!
    @IBOutlet weak var marketPriceLabel: UILabel!
    @IBOutlet weak var discountLabel: UILabel!
    @IBOutlet weak var img: UIImageView!
    
    
    var addShopCart:TouchBlock!
    
    var model:SiteGoodsModel!
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.xiajiaView.isHidden = true
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func reload(withModel model:SiteGoodsModel){
        self.model = model
        self.img.my_setImage(url: model.img, placeholder: Api.placeHolder.rawValue)
        self.nameLabel.text = model.name
        
        var currentPrice:Double = 0
        if model.isDiscount == 1 {
            currentPrice = model.discountMoney
        }else if model.isActivity == 1 {
            currentPrice = model.activityMoney
        }else if model.peddlersPrice != nil {
            currentPrice = Double.init((model.peddlersPrice)!)!
        }else{
            currentPrice = model.price
        }
        
        self.price.attributedText = Utils.changeLabelTextFontSize(normalString: "¥\(currentPrice)/斤", changeString: "\(currentPrice)".getPriceInteger(), font: UIFont.systemFont(ofSize: 17))
        self.marketPriceLabel.attributedText = Utils.labelTextDeleLine(normalString: "市场价：￥\(model.marketPrice)", deleString: "￥" + "\(model.marketPrice)")
        if model.state != 0 {
            self.xiajiaView.isHidden = false
        }else{
            self.xiajiaView.isHidden = true
        }
        
        if model.isDiscount == 1 {
            let disCount = String.init(format: "%.2f", model.discountMoney / model.price)
            self.discountLabel.isHidden = false
            self.discountLabel.text =  "  " + disCount + "折" + "  "
        }else {
            self.discountLabel.isHidden = true
        }
    }
    
    
    @IBAction func addMethod(_ sender: UIButton) {
        guard Utils.isLogined() else {
            if self.addShopCart != nil {
                self.addShopCart()
            }
            return
        }
        Utils.insertCart(goodsId: self.model.id) { (value) in
            if value.0 {
                PKhud.showText(text: "添加成功~")
            }else{
                PKhud.showText(text: value.1)
            }
        }
        
    }
    
}
